package com.haiyu.manager.controller.platform;


import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;

import com.haiyu.manager.dto.Category2DTO;
import com.haiyu.manager.dto.Category2SearchDTO;
import com.haiyu.manager.pojo.Category2;
import com.haiyu.manager.response.PageDataResult;
import com.haiyu.manager.service.Category2Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("category2")//指定路由
public class Category2Controller {

    @Autowired
    private Category2Service category2Service;
    private Logger logger = LoggerFactory.getLogger(this.getClass());



    /**
     *
     * 功能描述: 跳到Category2列表
     *
     *
     */
    @RequestMapping("/category2Manage")
    public String category2Manage() {
        return "/category2/category2Manage";
    }

    /**
     *
     * 功能描述: 分页查询Category2列表
     *
     *
     */
     @RequestMapping(value = "/getCategory2List", method = RequestMethod.POST)
    @ResponseBody
    public PageDataResult getCategory2List(@RequestParam("pageNum") Integer pageNum,
                                           @RequestParam("pageSize") Integer pageSize,/*@Valid PageRequest page,*/ Category2SearchDTO category2Search) {
        /*logger.info("分页查询用户列表！搜索条件：userSearch：" + userSearch + ",pageNum:" + page.getPageNum()
                + ",每页记录数量pageSize:" + page.getPageSize());*/
         System.out.println(category2Search);
        PageDataResult pdr = new PageDataResult();
        try {
            if(null == pageNum) {
                pageNum = 1;
            }
            if(null == pageSize) {
                pageSize = 10;
            }
            // 获取用户列表
            pdr = category2Service.getCategory2List(category2Search, pageNum ,pageSize);
            logger.info("Category2列表查询=pdr:" + pdr);

        } catch (Exception e) {
            e.printStackTrace();
            logger.error("Category2列表查询异常！", e);
        }
        return pdr;
    }


    /**
     *
     * 功能描述: 新增和更新Category2
     *
     *
     */
    @RequestMapping(value = "/setCategory2", method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> setCategory2(Category2 category2) {
        logger.info("设置category2[新增或更新]！category2:" + category2);

        System.out.println("category2");
        Map<String,Object> data = new HashMap();
        if(category2.getCategory2Id() == null){
            System.out.println("近来这里了吗？");
            data = category2Service.addCategory2(category2);
        }else{
            data = category2Service.updateCategory2(category2);
        }
        return data;
    }


    /**
     *
     * 功能描述: 删除/恢复 Category2
     *
     *
     */
    @RequestMapping(value = "/updateCategory2Status", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> updateCategory2Status(@RequestParam("category2Id") Integer category2Id,@RequestParam("category2Label") Integer category2Label) {
        logger.info("删除/恢复用户！category2Id:" + category2Id+" category2Label:"+category2Label);
        Map<String, Object> data = new HashMap<>();
        if(category2Label == 1){
            //删除用户
            data = category2Service.delCategory2(category2Id,category2Label);
        }else{
            //恢复用户
            data = category2Service.recoverCategory2(category2Id,category2Label);
        }
        return data;
    }


}
